package com.scau.ums.controller;

import com.scau.ums.model.Suggestion;
import com.scau.ums.model.User;
import com.scau.ums.service.CommonSuggest;
import com.scau.ums.service.LimitsSuggest;
import com.scau.ums.service.factory.SuggestImpFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author: 莫伟杰
 * date： 2019/7/22 10:30
 */
@Controller
public class SuggestController {

    @Autowired
    private SuggestImpFactory suggestImpFactory;

    @RequestMapping(value = "/new_suggestion",method = RequestMethod.GET)
    public String toNewSuggest()
    {
        return "newSuggestion";
    }
    @RequestMapping(value = "/new_suggestion",method = RequestMethod.POST)
    public String newSuggest(HttpServletRequest request)
    {
        String suggestion = request.getParameter("suggestion");
        String userID = (String)request.getSession().getAttribute("USER_ID");
        int type = (Integer)request.getSession().getAttribute("USER_TYPE");
        //从session获取用户ID和类型
        CommonSuggest suggestImp = suggestImpFactory.getSuggestImp(type);
        if(suggestImp.newSuggestions(userID,suggestion))
        {
            //添加进数据库成功
            System.out.println("发表成功！");
            return "suggestSucceed";
        }
        else
        {
            System.out.println("发表失败！");
            //添加进数据库失败
            return "redirect:index.jsp";
        }
    }


    //显示所有的建议包含未过审核的
    @RequestMapping("/verifySuggestionslist")
    @ResponseBody
    public List<Suggestion> showVerifySuggestions(ModelMap map, Integer passid, Integer rejectid){
        LimitsSuggest admins=(LimitsSuggest)suggestImpFactory.getSuggestImp(User.ADMIN);
        List<Suggestion> suggests=admins.getUnverifySuggestions();
        if (passid==null&&rejectid==null){
            map.put("verifySuggetionsList",suggests);
            return suggests;//返回未过审核的建议list的json串
        }
        else if(passid==null){
            admins.verifySuggestion(suggests.get(rejectid),CommonSuggest.STATE_NOTPASS);
            map.put("verifySuggetionsList",suggests);
            return suggests;//返回未过审核的建议list的json串
        }
        else{
            System.out.println("=============="+passid+CommonSuggest.STATE_PASS);
            admins.verifySuggestion(suggests.get(passid),CommonSuggest.STATE_PASS);
            map.put("verifySuggetionsList",suggests);
            return suggests;//返回未过审核的建议list的json串
        }


    }


    //显示所有的建议包含未过审核的
    @RequestMapping("/GetverifySuggestions")
    public String showAllSuggestion(){
        return "verifySuggestions";
    }


}
